package com.akeil.flash.action;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.akeil.flash.preferences.IPreferenceConstants;
import com.yang.serialport.exception.NoSuchPort;
import com.yang.serialport.exception.NotASerialPort;
import com.yang.serialport.exception.PortInUse;
import com.yang.serialport.exception.SerialPortParameterFailure;
import com.yang.serialport.exception.TooManyListeners;
import com.yang.serialport.manage.SerialPortManager;

import flash.Activator;
import flash.Application;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

public class ReOpenSerialPortAction extends AbstractFlashAction {
	private IPreferenceStore pre = Activator.getDefault().getPreferenceStore();
	
	public ReOpenSerialPortAction() {
		this.setImageDescriptor(
				AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/parameter/import_16px.png"));
		this.setToolTipText(resourceBundle.getString("toolbar.reopen.port.tooltip"));
	}

	@Override
	public void run() {
		reOpenSerialPort(null);		
		super.run();
	}
	
	private void reOpenSerialPort(java.awt.event.ActionEvent evt) {
		String commName = pre.getString(IPreferenceConstants.SERIAL_PORT);
		int baudrate = Integer.parseInt(pre.getString(IPreferenceConstants.SERIAL_BAUD));
		String databits = pre.getString(IPreferenceConstants.SERIAL_DATABITS);
		String stopbits = pre.getString(IPreferenceConstants.SERIAL_STOPBITS);
		String parity = pre.getString(IPreferenceConstants.SERIAL_PARITY);
		
		try {
			SerialPortManager.closePort(Application.serialport);
			
			Application.serialport = SerialPortManager.reOpenPort(commName, baudrate, databits,stopbits,parity);
			if (Application.serialport != null) {
				
			}
		} catch (SerialPortParameterFailure e) {
			e.printStackTrace();
		} catch (NotASerialPort e) {
			e.printStackTrace();
		} catch (NoSuchPort e) {
			//ShowUtils.errorMessage(e.toString());
		} catch (PortInUse e) {
			//ShowUtils.warningMessage("COM口已占用！");
		}

		try {
			SerialPortManager.addListener(Application.serialport, new SerialListener());
		} catch (TooManyListeners e) {
			e.printStackTrace();
		}
	}
	
	private class SerialListener implements SerialPortEventListener {
		
		public void serialEvent(SerialPortEvent serialPortEvent) {

			switch (serialPortEvent.getEventType()) {

			case SerialPortEvent.BI: // 通讯中断
				//ShowUtils.errorMessage("通信中断，请检查设备及连线！");
				break;

			case SerialPortEvent.OE: // 溢出错误

			case SerialPortEvent.FE: // 帧错误

			case SerialPortEvent.PE: // 奇数偶数校验错误

			case SerialPortEvent.CD: // 载波检测
				
			case SerialPortEvent.CTS: // 清除发送

			case SerialPortEvent.DSR: // 数据设备准备好

			case SerialPortEvent.RI: // 振铃指示

			case SerialPortEvent.OUTPUT_BUFFER_EMPTY: // 2 输出缓冲区已清空
				break;

			case SerialPortEvent.DATA_AVAILABLE: // 1串口数据已到达
				byte[] data = null;
				try {
					if (Application.serialport == null) {
						//ShowUtils.errorMessage("串口没有打开");
					} else {
						data = SerialPortManager.readFromPort(Application.serialport);
						if(data!= null && data.length > 0){
							//ConsoleHandler.info(ByteUtils.byteArrayToHexString(data,true));
							Application.processor.PushData(data);// 单管流程
						}
					}
				} catch (Exception e) {
					//ShowUtils.errorMessage(e.toString());
					// 
					System.exit(0);
				}
				break;
			}
		}
	}
}
